package com.wzc.org.service.impl;

import com.wzc.basic.exception.BusinessException;
import com.wzc.basic.service.impl.BaseServiceImpl;
import com.wzc.basic.util.BaiduAuditUtils;
import com.wzc.org.domain.Employee;
import com.wzc.org.domain.Shop;
import com.wzc.org.domain.ShopLog;
import com.wzc.org.dto.ShopDto;
import com.wzc.org.mapper.EmployeeMapper;
import com.wzc.org.mapper.ShopLogMapper;
import com.wzc.org.mapper.ShopMapper;
import com.wzc.org.service.IShopService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import java.util.List;


@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {

    @Autowired
    private ShopMapper shopMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private ShopLogMapper shopLogMapper;

    @Autowired
    private  JavaMailSender javaMailSender;


    /**
     * 实现店铺入驻流程
     * @param shop
     */
    @Override
    public void settlement(Shop shop){
        if(StringUtils.isEmpty(shop.getName())
                || StringUtils.isEmpty(shop.getAddress())
                || StringUtils.isEmpty(shop.getTel())
                || StringUtils.isEmpty(shop.getAdmin().getUsername())
                || StringUtils.isEmpty(shop.getAdmin().getPhone())
                || StringUtils.isEmpty(shop.getAdmin().getEmail())
                || StringUtils.isEmpty(shop.getAdmin().getPassword())
                || StringUtils.isEmpty(shop.getAdmin().getComfirmPassword())
        ){
            // 提示：参数不能为空
            throw new BusinessException("参数不能为空");
        }

        // 百度AI审核名称是否符合规范
        Boolean aBoolean = BaiduAuditUtils.TextCensor(shop.getName());
        if (!aBoolean){
            throw new BusinessException("名称不合规范！");
        }

        Employee admin = shop.getAdmin();
        // 校验两次密码是否一致
        if(!admin.getPassword().equals(admin.getComfirmPassword())){
            throw new BusinessException("密码不一致，请重新输入!");
        }

        // 校验店铺是否注册过
        Shop dbShop = shopMapper.getShopByMessage(shop);
        if(dbShop != null){
            throw new BusinessException("该店铺已经被注册过！");
        }

        // 将shop和employee数据保存在表中
        // 1. 保存employee, 获取自增的主键
        employeeMapper.save(admin);

        // 2. 把自增的主键设置到shop表中admin_id中
        shop.setAdmin_id(admin.getId());
        // 3. 保存shop数据，返回自增的主键
        shopMapper.save(shop);

        // 4. 3 返回的自增主键设置给employee的shop_id字段
        admin.setShop_id(shop.getId());
        // 5. 更新employee数据
        employeeMapper.update(admin);

    }

    /**
     * 审核通过业务处理
     * @param log
     * @throws MessagingException
     */
    @Override
    public void pass(ShopLog log) throws MessagingException {
        // 前端传参log携带数据  shop_id     note
        Shop shop = shopMapper.loadById(log.getShop_id()); // 通过shop_id查询shop对应的数据，再修改其状态
        shop.setState(2); // 审核通过，状态赋值为2（待激活）
        shopMapper.update(shop); // j将修改后的数据更新到数据库

        // 录入日志  shoplog
        log.setState(2); // 修改的状态
        log.setAudit_id(1L); // 审核人id
        shopLogMapper.save(log); // 保存数据

        // 通知商家， 发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("540827362@qq.com");
        helper.setSubject("店铺激活邮件");
        helper.setText("<h1>你的店铺已经注册!!!</h1><a href='http://localhost:8080/shop/active/" + log.getShop_id() +"'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("540827362@qq.com");
        javaMailSender.send(mimeMessage);
    }

    /**
     * 审核驳回业务处理
     * @param log
     * @throws MessagingException
     */
    @Override
    public void reject(ShopLog log) throws MessagingException {
        // 前端传参log携带数据  shop_id     note
        Shop shop = shopMapper.loadById(log.getShop_id()); // 通过shop_id查询shop对应的数据，再修改其状态
        shop.setState(4); // 审核驳回，状态赋值为4（驳回）
        shopMapper.update(shop); // 将修改后的数据更新到数据库

        // 录入日志  shoplog
        log.setState(4); // 修改的状态
        log.setAudit_id(1L); // 审核人id
        shopLogMapper.save(log); // 保存数据

        // 通知商家， 发送邮件
        MimeMessage mimeMessage = javaMailSender.createMimeMessage();
        //发送复杂邮件的工具类
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true,"utf-8");
        helper.setFrom("540827362@qq.com");
        helper.setSubject("驳回邮件");
        helper.setText("<h1>你的店铺入驻失败!!!</h1><a href='http://localhost:8080/shop/active/" + log.getShop_id() +"'>点击该链接激活</a>",true);
        //收件人
        Employee employee = employeeMapper.loadById(shop.getAdmin_id());
        String email = employee.getEmail();
        helper.setTo("540827362@qq.com");
        javaMailSender.send(mimeMessage);

    }


    /**
     * EchartsS数据表格
     * @return
     */
    @Override
    public List<ShopDto> getEcharts() {
        return shopMapper.getEcharts();
    }


}
